﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using Telerik.Web.UI;
using System.Threading;
using BSCMFF.Core.BusinessLayer;
using BSCMFF.Core.DataAccessLayer.DataObjects;
using BSCMFF.Core.DataAccessLayer;

namespace BSCMFF.Web
{
    public partial class MainSite : System.Web.UI.MasterPage
    {
        public CompanyInfo ci;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["loggedUserID"] == null)
                {
                    string login = HttpContext.Current.User.Identity.Name;

                    BSCEntities context = new BSCEntities();

                    User user = (from u in context.Users
                                 where u.Login == login
                                 select u).SingleOrDefault();

                    if (user != null)
                    {
                        Session.Add("loggedUserID", user.ID);
                    }
                }
                
                string role = ShowRoles();

                AdjustMenuVisibilityAccordintToRole(role);

                KeepPanelItemOpen();

                //Literal1.Text = String.Format("This is the <em>{0}</em> page", pageContentString);

                ShowLogo();

                HttpCookie cookie = Request.Cookies["Theme"];
                if (cookie != null)
                {
                    RadSkinManager1.Skin = cookie.Value;
                }
                
            }
        }

        private void AdjustMenuVisibilityAccordintToRole(string role)
        {
            switch (role)
            {
                case "Administrator": AdjustAdmin(); break;
                case "Manager": AdjustManager(); break;
                case "Personalist": AdjustPersonalist(); break;
                case "Employee": AdjustEmployee(); break;
                default: break;
            }
        }

        private void AdjustEmployee()
        {
            RadPanelBar1.FindItemByValue("Admin").Visible = false;

            RadPanelBar1.FindItemByValue("BSC").Items.FindItemByValue("BusinessGoalWizard").Visible = false;
            RadPanelBar1.FindItemByValue("BSC").Items.FindItemByValue("TargetsManagement").Visible = false;
            RadPanelBar1.FindItemByValue("BSC").Items.FindItemByValue("StrategyMap").Visible = false;
            RadPanelBar1.FindItemByValue("BSC").Items.FindItemByValue("BSCProgress").Visible = false;
            RadPanelBar1.FindItemByValue("BSC").Items.FindItemByValue("BulkCascade").Visible = false;

            RadPanelBar1.FindItemByValue("Survey").Items.FindItemByValue("ManageSurveys").Visible = false;

            RadPanelBar1.FindItemByValue("Evaluation").Items.FindItemByValue("ManageEvaluations").Visible = false;
            RadPanelBar1.FindItemByValue("Evaluation").Items.FindItemByValue("EvaluationNotifications").Visible = false;
            RadPanelBar1.FindItemByValue("Evaluation").Items.FindItemByValue("AssignEmployeesforEvaluation").Visible = false;
        }

        private void AdjustPersonalist()
        {
            RadPanelBar1.FindItemByValue("Admin").Visible = false;

            RadPanelBar1.FindItemByValue("BSC").Items.FindItemByValue("BusinessGoalWizard").Visible = false;
            RadPanelBar1.FindItemByValue("BSC").Items.FindItemByValue("TargetsManagement").Visible = false;
            RadPanelBar1.FindItemByValue("BSC").Items.FindItemByValue("StrategyMap").Visible = false;
            RadPanelBar1.FindItemByValue("BSC").Items.FindItemByValue("BSCProgress").Visible = false;
            RadPanelBar1.FindItemByValue("BSC").Items.FindItemByValue("BulkCascade").Visible = false;
        }

        private void AdjustManager()
        {
            RadPanelBar1.FindItemByValue("Admin").Visible = false;

            RadPanelBar1.FindItemByValue("Survey").Items.FindItemByValue("ManageSurveys").Visible = false;

            RadPanelBar1.FindItemByValue("Evaluation").Items.FindItemByValue("ManageEvaluations").Visible = false;
            RadPanelBar1.FindItemByValue("Evaluation").Items.FindItemByValue("EvaluationNotifications").Visible = false;
            RadPanelBar1.FindItemByValue("Evaluation").Items.FindItemByValue("AssignEmployeesforEvaluation").Visible = false;
        }

        private void AdjustAdmin()
        {
            RadPanelBar1.FindItemByValue("BSC").Items.FindItemByValue("BusinessGoalWizard").Visible=false;
            RadPanelBar1.FindItemByValue("BSC").Items.FindItemByValue("TargetsManagement").Visible = false;
            RadPanelBar1.FindItemByValue("BSC").Items.FindItemByValue("StrategyMap").Visible = false;
            RadPanelBar1.FindItemByValue("BSC").Items.FindItemByValue("BSCProgress").Visible = false;
            RadPanelBar1.FindItemByValue("BSC").Items.FindItemByValue("BulkCascade").Visible = false;

            RadPanelBar1.FindItemByValue("Survey").Items.FindItemByValue("ManageSurveys").Visible = false;

            RadPanelBar1.FindItemByValue("Evaluation").Items.FindItemByValue("ManageEvaluations").Visible = false;
            RadPanelBar1.FindItemByValue("Evaluation").Items.FindItemByValue("EvaluationNotifications").Visible = false;
            RadPanelBar1.FindItemByValue("Evaluation").Items.FindItemByValue("AssignEmployeesforEvaluation").Visible = false;
        }

        private void ShowLogo()
        {
            if (!IsPostBack)
            {
                CompanyInfoManager cim = new CompanyInfoManager();

                ci = cim.GetCompanyInfo();
                RadBinaryImageMasterLogo.DataValue = ci.CompanyLogo;
                RadBinaryImageMasterLogo.DataBind();
            }
        }

        private string ShowRoles()
        {
            string[] roles = Roles.GetRolesForUser();

            LabelUserRoles.Text = "";

            foreach (string r in roles)
            {
                LabelUserRoles.Text += r + " ";

                HideMenuItemsForRole(r);
            }
            return roles[0];
        }

        private void HideMenuItemsForRole(string r)
        {
            switch (r)
            {
                case "Administrator": ShowAdmin(); break;
                case "Manager": ShowManager(); break;
                case "Employee": ShowEmployee(); break;
                default: break;
            }
        }

        private void ShowEmployee()
        {
            RadPanelItem rpi = RadPanelBar1.Items.FindItemByValue("Admin");
            rpi.Visible = false;
        }

        private void ShowManager()
        {
            //throw new NotImplementedException();
        }

        private void ShowAdmin()
        {
            //throw new NotImplementedException();
        }

        private void KeepPanelItemOpen()
        {
            // ApplyAppPathModifier will add the session ID if we're using Cookieless session.
            string urlWithSessionID = Response.ApplyAppPathModifier(Request.Url.PathAndQuery);
            RadPanelItem clickedItem = RadPanelBar1.FindItemByUrl(urlWithSessionID);

            // Expand the parent of the clicked item
            if (clickedItem != null)
            {
                clickedItem.ExpandParentItems();
                ShowPath(clickedItem);
            }

            //string pageContentString;
            //if (Request.QueryString["page"] == null)
            //{
            //    pageContentString = "home";
            //}
            //else
            //{
            //    pageContentString = Request.QueryString["page"];
            //}
        }



        private void ShowPath(RadPanelItem clickedItem)
        {
            foreach (RadPanelItem childItem in clickedItem.PanelBar.GetAllItems())
            {
                childItem.CssClass = "";
            }

            clickedItem.CssClass = "rpSelected";
        }

        protected void ImageButtonEnglish_Click(object sender, ImageClickEventArgs e)
        {
            HttpCookie cookie = new HttpCookie("Culture");
            cookie.Value = "en-US";
            Response.Cookies.Add(cookie);

            Response.Redirect(Request.Url.AbsolutePath);
        }

        protected void ImageButtonSlovak_Click(object sender, ImageClickEventArgs e)
        {
            HttpCookie cookie = new HttpCookie("Culture");
            cookie.Value = "sk-SK";
            Response.Cookies.Add(cookie);

            Response.Redirect(Request.Url.AbsolutePath);

            //if (Session["culture"] == null)
            //{
            //    Session.Add("culture", "sk-SK");
            //}
            //else
            //{
            //    Session["culture"] = "sk-SK";
            //}


            //Response.Redirect(Request.Url.AbsolutePath);
        }

        protected void LoginStatus1_LoggingOut(object sender, LoginCancelEventArgs e)
        {
            Session.Abandon();
        }

        //protected override void InitializeCulture()
        //{
        //    String selectedLanguage;

        //    HttpCookie cookie = Request.Cookies["Culture"];
        //    if (cookie != null)
        //    {
        //        selectedLanguage = cookie.Value;
        //    }
        //    else
        //    {
        //        selectedLanguage = "en-US";
        //    }

            
        //    MainContent.Page.UICulture = selectedLanguage;
        //    MainContent.Page.Culture = selectedLanguage;

        //    //Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(selectedLanguage);
        //    //Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedLanguage);

        //    //MainContent.Page.
        //    //base.InitializeCulture();
        //}
    }
}